package com.cash.app.ui.main.fg.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.cash.app.databinding.FgHomeGridItemBinding

class HomeGridAdapter : BaseAdapter() {
    private var items: List<GridItem> = emptyList()

    fun setData(data: List<GridItem>) {
        items = data
        notifyDataSetChanged()
    }

    override fun getCount(): Int = items.size
    override fun getItem(position: Int): GridItem = items[position]
    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val itemBinding = if (convertView == null) {
            FgHomeGridItemBinding.inflate(LayoutInflater.from(parent?.context), parent, false)
        } else {
            FgHomeGridItemBinding.bind(convertView)
        }

        val item = getItem(position)
        itemBinding.apply {
            ivIcon.setImageResource(item.iconResId)
            tvTitle.text = item.title
            tvDesc.text = item.desc
        }

        return itemBinding.root
    }
}

data class GridItem(
    val iconResId: Int, val title: String, val desc: String
)